/**********************************************************************************
 * Copyright (c) 2012 - 2012,  Collect4PNP, All right reserved
 * Title: com.cipas.pnp.jdbc  DBConstants.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-7-11 上午11:03:29 
 *********************************************************************************/


package com.adc.jdbc.constants;

import java.io.File;
import java.util.Properties;

import com.adc.quicktools.PropertiesUtil;
import com.adc.quicktools.StringUtil;
import com.adc.quicktools.file.FileControl;
import com.jolbox.bonecp.BoneCPConfig;



public class DBConstants {
	public final static String DefaultPoolConfigName="bonecp.properties";
	public final static String DB_LIST_CFG="dblist.properties";
	
	public final static String DB_CFG_DRIVER="driverName";
	public final static String DB_CFG_URL="jdbcUrl";
	public final static String DB_CFG_USER="username";
	public final static String DB_CFG_PASSWORD="password";
	
	private final static String DB_CFG_MASK="db.conf";
	private static String basdir;
	private final static String CFG_Suffix=".properties";
	
	/**
	 * 
	 * Description: make out Name in DBList config by parameter "i"
	 * author:  shen
	 * @param i 
	 * @return String
	 */
	public static String getDBConfigName(int i) {
		return DB_CFG_MASK+i;
	}
	
	/**
	 * 
	 * Description: 加载默认的数据库连接池的配置
	 * author:  shen
	 * @return Properties
	 */
	private static Properties defaultCfg=null;
	public static Properties loadDefaultConfig() {
		if (defaultCfg==null) {
			defaultCfg=PropertiesUtil.loadProperties(getConfigPathByName(DefaultPoolConfigName));
		}
		return defaultCfg;
	}

	/**
	 * Description: when you want add a new dbconfig information to globalMap in runtime;
	 * you can use this method to make out the next name in DBList config;
	 * author:  shen 
	 * @return String
	 */
	public synchronized static String getDBConfigNextName() {
		Properties properties=PropertiesUtil.loadProperties(getConfigPathByName(DB_LIST_CFG));
		int size=properties.size()+1;
		System.out.println("The Next name in DBList config is "+DB_CFG_MASK+size);
		return DB_CFG_MASK+size;
	}
	
	/**
	 * 
	 * Description:  print out DB config  information
	 * author:  shen
	 * @param boneCPConfig 
	 * @return void
	 */
	public static void printDBConfig(BoneCPConfig boneCPConfig) {
		System.out.println("==Config Info=============");
//		System.out.println(boneCPConfig.toString());
		System.out.println("MinConnectionsPerPartition:"+boneCPConfig.getMinConnectionsPerPartition());
		System.out.println("MaxConnectionsPerPartition:"+boneCPConfig.getMaxConnectionsPerPartition());
		System.out.println("PartitionCount:"+boneCPConfig.getPartitionCount());
		System.out.println("ConnectionTimeoutInMs:"+boneCPConfig.getConnectionTimeoutInMs());
		System.out.println("==OVER====================");
	}
	
	/**
	 * 
	 * Description: 加载运行根路径
	 * author:  shen
	 * @return String
	 */
	public static String getCurrentBaseDir() {
		if (!StringUtil.checkStringValue(basdir)) {
			basdir=new File(FileControl.getAppPath(DBConstants.class)).getAbsolutePath().replace("\\", "/");
		}
		return basdir;
	}
	
	/**
	 * 
	 * Description: 根据配置文件的名称拼装配置文件全路径
	 * author:  shen
	 * @param name
	 * @return String
	 */
	public static String getConfigPathByName(String name) {
		if (StringUtil.checkStringValue(name)) {
			if (!name.endsWith(CFG_Suffix)) {
				name=name.concat(CFG_Suffix);
			}
			return getCurrentBaseDir().concat("/config/"+name);
		}
		return null;
	}
}
